package com.mak001.lite.multiSkill.subs;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Skills;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

public class RuneCrafterLite extends subScript {

	Area bankArea;
	Area runeArea;

	boolean switchB;

	int[] runes = { 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565,
			566 };
	int tally;
	int[] essID = { 1436, 7936 };
	int ess;
	GameObject OAlterObj;
	GameObject portalObj;

	private enum State {
		TOBANK, CRAFT, TORUNES, PORTAL, TELE, BANK
	}

	private State getState() {
		if (Inventory.getCount(ess) > 0) {
			if (Alter() != null) {
				return State.CRAFT;
			} else {
				if (OAlterObj != null) {
					if (OAlterObj.getLocation().isOnScreen()) {
						return State.TELE;
					} else {
						return State.TORUNES;
					}
				} else {
					return State.TORUNES;
				}
			}
		} else {
			if (bankArea.contains(getLoc())) {
				return State.BANK;
			} else {
				if (portalObj != null && portalObj.getLocation().isOnMap()) {
					if (runeArea.contains(Players.getLocal().getLocation())) {
						return State.TOBANK;
					} else {
						return State.PORTAL;
					}
				} else {
					return State.TOBANK;
				}
			}
		}
	}

	public int ess() {
		if (!Bank.isOpen()) {
			Bank.open();
		} else {
			if (Bank.getCount(essID[1]) > 0) {
				return essID[1];
			}
		}
		return essID[0];
	}

	private GameObject OAlter() {
		int[] id = { 2452, 2455, 2457, 2453, 2454, 2456 };// TODO
		return Objects.getNearest(id);
	}

	private GameObject Alter() {
		int[] id = { 2478, 2481, 2483, 2479, 2480, 2482 };// TODO
		return Objects.getNearest(id);
	}

	private GameObject portal() {
		int id[] = { 2465, 2468, 2470, 2466, 2467, 2469 };// TODO
		return Objects.getNearest(id);
	}

	private Tile getLoc() {
		return Players.getLocal().getLocation();
	}

	@Override
	public void execute() {
		if (bankArea == null) {
			if (tally == 1448) {
				bankArea = Methods.FaladorWest;
			} else {
				bankArea = Methods.getNearestBank(runeArea.getCentralTile());
			}
		}
		switch (getState()) {
		case TOBANK:
			Walking.findPath(bankArea.getCentralTile()).traverse();
			break;
		case BANK:
			setSwitch(true);
			if (Bank.isOpen()) {
				if (ess == 0 || ess == -1) {
					ess = ess();
				} else {
					if (Inventory.containsOneOf(runes)) {
						Bank.depositAllExcept(tally);
					} else {
						if (Bank.getCount(ess()) > 0) {
							Bank.withdraw(ess(), 0);
						} else {
							Methods.stopSub();
						}
					}
				}
			} else {
				Bank.open();
			}
			break;
		case TORUNES:
			setSwitch(false);
			Walking.findPath(runeArea.getCentralTile()).traverse();
			if (OAlter() != null && OAlterObj == null) {
				OAlterObj = OAlter();
			}

			break;
		case PORTAL:
			if (!Players.getLocal().isMoving()
					&& Players.getLocal().getAnimation() == -1) {
				if (portalObj.interact("")) {
					sleep(1000);
				} else {
					Walking.findPath(portalObj.getLocation()).traverse();
				}
			}
			break;
		case TELE:
			if (!Players.getLocal().isMoving()
					&& Players.getLocal().getAnimation() == -1) {
				if (OAlterObj.isOnScreen()) {
					if (Methods.useItem(Inventory.getItem(tally), OAlterObj)) {
						sleep(1000);
					}
				} else {
					Walking.findPath(OAlterObj).traverse();
				}
			}
			break;
		case CRAFT:
			if (!Players.getLocal().isMoving()
					&& Players.getLocal().getAnimation() == -1) {
				if (Players.getLocal().getAnimation() == -1) {
					if (Alter().interact("")) {
						sleep(1000);
					} else {
						Walking.findPath(Alter()).traverse();
					}
				}
			}
			if (portal() != null && portalObj == null) {
				portalObj = portal();
			}
			break;
		}
	}

	

	@Override
	public int[] required() {
		int[] req = { tally };
		// TODO Auto-generated method stub
		return req;
	}

	@Override
	public boolean canSwitch() {
		return switchB;
	}

	public void setSwitch(boolean b) {
		if (switchB != b)
			switchB = b;
	}

	public void setrune(Area rune) {
		this.runeArea = rune;
	}

	public void setTally(int i) {
		this.tally = i;
	}

	@Override
	public String getStatus() {
		if (getState() == State.PORTAL) {
			return "Enterring portal";
		} else if (getState() == State.BANK) {
			return "Banking";
		} else if (getState() == State.CRAFT) {
			return "Crafting";
		} else if (getState() == State.TELE) {
			return "Entering runes";
		} else if (getState() == State.TOBANK) {
			return "Walking to bank";
		} else {
			return "Walking to runes";
		}
	}

	@Override
	public int skill() {
		return Skills.RUNECRAFTING;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		return null;
	}

}
